package com.example.ccj.u.text;

import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.method.NumberKeyListener;
import android.util.Log;
import android.widget.EditText;

import androidx.annotation.NonNull;


public class TextInputType {
    public static final char[] intChar = new char[]{'0','1','2','3','4','5','6','7','8','9'};
    public static final char[] locationChar = new char[]{'0','1','2','3','4','5','6','7','8','9','.',','};
    public static final char[] zimu = new char[]{'a','b','c','d','e','f','g','7','8','9','.',','};


    public static void setEditTextInPutTypeToUser(EditText editText){
        editText.setFilters(new InputFilter[]{getZiMuInputFilter(20)});
    }

    public static void setEditTextInPutTypeToPhone(EditText editText){
        editText.setKeyListener(getIntKeyListener(intChar, InputType.TYPE_CLASS_NUMBER));
        editText.setFilters(new InputFilter[]{getIntInputFilter(11)});
    }

    public static void setEditTextInPutTypeToInt(EditText editText,int maxNumLength){
        editText.setKeyListener(getIntKeyListener(intChar, InputType.TYPE_CLASS_NUMBER));
        editText.setFilters(new InputFilter[]{getIntInputFilter(maxNumLength)});
    }

    public static void setEditTextInPutTypeToInt(EditText editText){
        editText.setKeyListener(getIntKeyListener(intChar, InputType.TYPE_CLASS_NUMBER));
        editText.setFilters(new InputFilter[]{getIntInputFilter(11)});
    }

    public static void setEditTextInPutTypeToDouble(EditText editText){
        setEditTextInPutTypeToDouble(editText,18,2,null,InputType.TYPE_MASK_VARIATION);
    }

    public static void setEditTextInPutTypeToDouble(EditText editText,int maxNumLength, int maxLittleLength){
        setEditTextInPutTypeToDouble(editText,maxNumLength,maxLittleLength,null,InputType.TYPE_MASK_VARIATION);
    }

    /**
     * 小数输入
     * @param maxNumLength 最大长度
     * @param maxLittleLength 小数最大长度
     */
    public static void setEditTextInPutTypeToDouble(EditText editText, int maxNumLength, int maxLittleLength, char[] chars, int type) {
        editText.setFilters(new InputFilter[]{getDoubleInputFilter(maxNumLength,maxLittleLength)});
        editText.setKeyListener(getDoubleKeyListener(chars, type));
    }

    /**
     * 整数输入
     * @param maxNumLength 最大长度
     * @return
     */
    public static InputFilter getIntInputFilter(int maxNumLength){
        return new InputFilter() {
            /**
             *
             * @param source 输入数据
             * @param start
             * @param end
             * @param dest 历史数据
             * @param dstart  输入位置
             * @param dend 输入结束位置
             * @return
             */
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if ("".equals(source.toString())) {
                    return null;
                }
                String dValue = dest.toString();
                //数据长度为0并且当前输入长度为1时直接录入
                if (dValue.length() < maxNumLength) {
                    if (source.length() == 1) {
                        return source;
                    } else {
                        int i = maxNumLength - dValue.length();
                        if (i >= source.length()) {
                            return source;
                        } else {
                            return source.toString().substring(source.length() - i);
                        }
                    }
                }
                return "";
            }
        };
    }

    private static final String TAG = "MyInputType";
    /**
     * 输入字母数字
     * @param maxNumLength 最大长度
     * @return
     */
    public static InputFilter getZiMuInputFilter(int maxNumLength){
        return new InputFilter() {
            /**
             *
             * @param source 输入数据
             * @param start
             * @param end
             * @param dest 历史数据
             * @param dstart  输入位置
             * @param dend 输入结束位置
             * @return
             */
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if ("".equals(source.toString())) {
                    return null;
                }

                String dValue = dest.toString();
                //数据长度为0并且当前输入长度为1时直接录入
                if (dValue.length() < maxNumLength) {

                    if (source.length() == 1) {

                        char one = source.charAt(0);
                        //数字
                        if (one >= 48 && one <= 57) {
                            return source;
                        }
                        //大写字母
                        if (one >= 65 && one <= 90) {
                            return source;
                        }
                        //小写字母
                        if (one >= 97 && one <= 122) {
                            return source;
                        }
                    } else {

                        //如果当前输入的是多个字符
                        int nowMax = maxNumLength - dValue.length();
                        StringBuilder builder = new StringBuilder();
                        //遍历出来符合条件的字符
                        for (int i = 0; i < source.length(); i++) {
                            char thisC = source.charAt(i);
                            if (isFuHe(thisC)) {
                                builder.append(thisC);
                            }
                        }

                        if (nowMax >= builder.length()) {

                            return builder;
                        } else {
                            return builder.substring(builder.length()-nowMax);
                        }
                    }
                }
                return "";
            }
        };
    }

    private static boolean isFuHe(char c){
        //数字
        if (c >= 48 && c <= 57) {
            return true;
        }
        //大写字母
        if (c >= 65 && c <= 90) {
            return true;
        }
        //小写字母
        if (c >= 97 && c <= 122) {
            return true;
        }
        return false;
    }




    public static InputFilter getDoubleInputFilter(int maxNumLength, int maxLittleLength){
        return new InputFilter() {
            /**
             *
             * @param source 输入数据
             * @param start
             * @param end
             * @param dest 历史数据
             * @param dstart  输入位置
             * @param dend 输入结束位置
             * @return
             */
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if ("".equals(source.toString())) {
                    return null;
                }
                String dValue = dest.toString();
                //数据长度为0并且当前输入长度为1时直接录入
                if (dValue.length() == 0 && source.length() < 2) {
                    return source;
                }
                //首先保证不让输入两个小数点
                int last = dValue.lastIndexOf(".");
                if (last > 0 && source.equals(".")) {
                    return  "";
                }

                //点的位置为-1说明没有输入点，并且当前输入的不是 . 那么检查长度
                if (last == -1) {
                    //如果当前输入的是.直接返回
                    if (source.equals(".")) {
                        return source;
                    }
                    //不存在 . 当前输入的不是点时检查长度，如果小于18位直接返回
                    if (dValue.length() < maxNumLength) {
                        return source;
                    }
                } else {
                    String[] split = dValue.split("\\.");
                    int zs = split[0].length();//整数长度
                    int xs ;//小数长度
                    if (split.length < 2) {
                        xs = 0;//小数长度
                    } else {
                        xs = split[1].length();//小数长度
                    }
                    //输入位置小于点的位置证明在点前面输入的
                    if (dstart <= last) {
                        if (zs < maxNumLength) {
                            return source;
                        }
                    } else {
                        if (xs < maxLittleLength) {
                            return source;
                        }
                    }
                }
                return "";
            }
        };
    }

    public static InputFilter getLocationInputFilter(int maxNumLength, int maxLittleLength){
        return new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if ("".equals(source.toString())) {
                    return null;
                }
                String dValue = dest.toString();
                //数据长度为0并且当前输入长度为1时直接录入
                if (dValue.length() == 0 && source.length() < 2) {
                    return source;
                }
                //首先保证不让输入两个逗号
                int last = dValue.lastIndexOf(",");
                if (last > 0 && source.equals(",")) {
                    return  "";
                }

                //当一位一位输入时
                if (source.length() == 1) {
                    //分割之前的字符串
                    String[] dh = dValue.split(",");
                    //之前有使用，分割
                    if (dh.length == 2) {
                        String lon = dh[0];//经度
                        String lat = dh[1];//纬度

                        String[] lons = lon.split("\\.");
                        String[] lats = lat.split("\\.");
                        int lonsXH = 0;
                        int lonsZH = 0;
                        if (lons.length == 2 && !source.equals(".")) {
                            //获取点位置
                            int lonsLast = dValue.lastIndexOf(".");
                            lonsXH = lons[0].length();
                            lonsZH = lons[1].length();
                            if (dstart <= lonsLast) {
                                //在点前面输入的
                                if (lonsXH < 3) return source;
                            } else {
                                if (lonsZH < 6) return source;
                            }
                        } else {
                            if (lon.length() < 3) return source;
                        }
                    } else {
                        if (dValue.length() < 3) return source;
                    }


                }



                //逗号的下标为-1证明还没输入
                if (last == -1) {
                    if (source.equals(",")) {
                        Log.d("经纬度输入", "filter: 没有逗号，输入逗号,在长度大于3位时在输入的位置进行分割");
                        if (dValue.length() >= 3) {
                            String front = dValue.substring(0,dstart);
                            String after = dValue.substring(dstart);




                        } else {
                            return source;
                        }


                        return source;
                    }
                    //当前不包含，输入的不是，尝试获取.



                }


                return "";
            }
        };
    }

    /**
     * 只能输入数字
     * @return
     */
    public static NumberKeyListener getIntKeyListener(char[] chars, int type){
        return new NumberKeyListener() {
            @NonNull
            @Override
            protected char[] getAcceptedChars() {
                if (chars != null) return chars;
                return new char[]{'1','2','3','4','5','6','7','8','9','0'};
            }

            @Override
            public int getInputType() {
                return type;
            }
        };

    }


    /**
     * 只能输入数字和点
     * @return
     */
    public static NumberKeyListener getDoubleKeyListener(char[] chars, int type){
        return new NumberKeyListener() {
            @NonNull
            @Override
            protected char[] getAcceptedChars() {
                if (chars != null) return chars;
                return new char[]{'1','2','3','4','5','6','7','8','9','0','.'};
            }

            @Override
            public int getInputType() {
                return type;
            }
        };

    }
}
